using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ctem.gui
{
    public partial class CommonWForm : Form
    {
        public string NameCommonW = "";
        public List<TextBox> tb_numerator = new List<TextBox>();
        List<Label> lb_numerator = new List<Label>();
        public List<TextBox> tb_denominator = new List<TextBox>();
        List<Label> lb_denominator = new List<Label>();

        public ctem.kernel.adbms.IModeller modeller;

        private void DeleteLastFromNumerator()
        {

            if ((tb_numerator.Count > 0) && (lb_numerator.Count > 0))
            {
                this.Controls.Remove(tb_numerator[tb_numerator.Count - 1]);
                tb_numerator.RemoveAt(tb_numerator.Count - 1);

                this.Controls.Remove(lb_numerator[lb_numerator.Count - 1]);
                lb_numerator.RemoveAt(lb_numerator.Count - 1);

                if (lb_numerator.Count > 0)
                {
                    if (lb_numerator.Count == 1)
                        lb_numerator[lb_numerator.Count - 1].Text = "";
                    else
                        if (lb_numerator.Count == 2)
                            lb_numerator[lb_numerator.Count - 1].Text = "s";
                        else
                            lb_numerator[lb_numerator.Count - 1].Text = "s^" + (lb_numerator.Count - 1).ToString();// +"+";
                }

            }
        }

        private void DeleteLastFromDenominator()
        {

            if ((tb_denominator.Count > 0) && (lb_denominator.Count > 0))
            {
                this.Controls.Remove(tb_denominator[tb_denominator.Count - 1]);
                tb_denominator.RemoveAt(tb_denominator.Count - 1);


                this.Controls.Remove(lb_denominator[lb_denominator.Count - 1]);
                lb_denominator.RemoveAt(lb_denominator.Count - 1);
                if (lb_denominator.Count > 0)
                {
                    if (lb_denominator.Count == 1)
                        lb_denominator[lb_denominator.Count - 1].Text = "";
                    else
                        if (lb_denominator.Count == 2)
                            lb_denominator[lb_denominator.Count - 1].Text = "s";
                        else
                            lb_denominator[lb_denominator.Count - 1].Text = "s^" + (lb_denominator.Count - 1).ToString();// +"+";
                }
            }
        }


        private void AddTBToNumerator(string val)
        {

            TextBox tb_new = new TextBox();
            tb_new.Parent = this;
            tb_new.Left = 140 + tb_numerator.Count * 60;
            tb_new.Top = 20;
            tb_new.Width = 30;
            tb_new.Height = 10;
            tb_new.Text = val;
            tb_new.Visible = true;
            tb_new.Name = "tb" + tb_numerator.Count.ToString();
            tb_numerator.Add(tb_new);
            this.Controls.Add(tb_new);


            Label lb_new = new Label();
            lb_new.Parent = this;
            lb_new.Left = 170 + lb_numerator.Count * 60;
            lb_new.Top = 20;
            if (lb_numerator.Count==0)
                lb_new.Text = "";
            else
                if (lb_numerator.Count==1)
                    lb_new.Text = "s";
                else
                    lb_new.Text = "s^" + lb_numerator.Count.ToString();// +"+";
            if (lb_numerator.Count>0)
                lb_numerator[lb_numerator.Count - 1].Text += "+";
            lb_new.Width = 30;
            lb_new.Height = 20;
            lb_new.Visible = true;
            lb_new.Name = "lb" + lb_numerator.Count.ToString();
            lb_numerator.Add(lb_new);
            this.Controls.Add(lb_new);
        }

        private void AddTBToDenominator(string val)
        {
            TextBox tb_new = new TextBox();
            tb_new.Parent = this;
            tb_new.Left = 140 + tb_denominator.Count * 60;
            tb_new.Top = 70;
            tb_new.Width = 30;
            tb_new.Height = 10;
            tb_new.Text = val;
            tb_new.Visible = true;
            tb_new.Name = "tb2" + tb_denominator.Count.ToString();
            tb_denominator.Add(tb_new);
            this.Controls.Add(tb_new);


            Label lb_new = new Label();
            lb_new.Parent = this;
            lb_new.Left = 170 + lb_denominator.Count * 60;
            lb_new.Top = 70;
            if (lb_denominator.Count == 0)
                lb_new.Text = "";
            else
                if (lb_denominator.Count == 1)
                    lb_new.Text = "s";
                else
                    lb_new.Text = "s^" + lb_denominator.Count.ToString();// +"+";
            if (lb_denominator.Count > 0)
                lb_denominator[lb_denominator.Count - 1].Text += "+";
            lb_new.Width = 30;
            lb_new.Height = 20;
            lb_new.Visible = true;
            lb_new.Name = "lb2" + lb_denominator.Count.ToString();
            lb_denominator.Add(lb_new);
            this.Controls.Add(lb_new);
        }



        public CommonWForm()
        {
            InitializeComponent();
            tb_numerator.Clear();
            tb_denominator.Clear();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            string tt_str="";
            for (int i = 0; i < tb_numerator.Count; i++)
            //for (int i = tb_numerator.Count-1; i >= 0; i--)
                tt_str += tb_numerator[i].Text + "|";
            if (tb_denominator.Count > tb_numerator.Count)
                for (int i = tb_numerator.Count; i < tb_denominator.Count; i++)
                    tt_str += "0,0|";

            modeller.AsyncSendCommand("set param " + NameCommonW + ".strb "+'\'' + tt_str+'\'');

            tt_str = "";
            for (int i = 0; i < tb_denominator.Count; i++)
            //for (int i = tb_denominator.Count - 1; i >= 0; i--)
                tt_str += tb_denominator[i].Text + "|";
            modeller.AsyncSendCommand("set param " + NameCommonW + ".stra " + '\'' + tt_str + '\'');

            Close();
        }

        private void CommonWForm_Paint(object sender, PaintEventArgs e)
        {
            //foreach (TextBox tb in tb_numerator)
            //    tb.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tb_numerator.Count < tb_denominator.Count)
                AddTBToNumerator("0,0");
        }

        private void btnAddDenom_Click(object sender, EventArgs e)
        {
            AddTBToDenominator("0,0");
        }

        private void CommonWForm_Load(object sender, EventArgs e)
        {
            string tt_str = modeller.SyncSendCommand<string>("get param value " + NameCommonW + ".strb");

            char[] delim = new char[1];
            delim[0] = '|';
            string[] temp_num = tt_str.Split(delim);
            for (int i = 0; i < temp_num.GetLength(0)-1; i++)
            {
                AddTBToNumerator(temp_num[i]);
            }


            //Array.Reverse(temp_num);
            //for (int i = 1; i < temp_num.GetLength(0); i++)
            //{
            //    AddTBToNumerator(temp_num[i]);
            //}



            tt_str = modeller.SyncSendCommand<string>("get param value " + NameCommonW + ".stra");
            temp_num = tt_str.Split(delim);

            for (int i = 0; i < temp_num.GetLength(0)-1; i++)
            {
                AddTBToDenominator(temp_num[i]);
            }

            //Array.Reverse(temp_num);
            //for (int i = 1; i < temp_num.GetLength(0); i++)
            //{
            //    AddTBToDenominator(temp_num[i]);
            //}


        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            DeleteLastFromNumerator();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (tb_numerator.Count < tb_denominator.Count)
                DeleteLastFromDenominator();
        }
    }
}